﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.Entities;
using Unity.Transforms;
using Unity.Mathematics;
using Unity.Jobs;
using Unity.Burst;
using Ecset = Unity.Entities.Entity;

[BurstCompile]
public class Fade : ComponentSystem
{
    EndSimulationEntityCommandBufferSystem m_EndSimulationEntityCommandBufferSystem;
    protected override void OnCreate()
    {
        base.OnCreate();
        //mGroup = GetEntityQuery(typeof(VoxelComponentData), typeof(FadeTagComponentData));
        m_EndSimulationEntityCommandBufferSystem =
            World.GetOrCreateSystem<EndSimulationEntityCommandBufferSystem>();
    }

    public void DeleteAsync()
    {
        var ecb = m_EndSimulationEntityCommandBufferSystem.CreateCommandBuffer();
        Entities.WithAll<FadeTagComponentData>().ForEach((Ecset entity) =>
        {
            ecb.DestroyEntity(entity);
        });
    }

    protected override void OnUpdate()
    {
        if (TerrianManager.waitForDelete == false)
            return;
        var ecb = m_EndSimulationEntityCommandBufferSystem.CreateCommandBuffer();

        Entities.WithAll<FadeTagComponentData>().ForEach((Ecset entity, ref VoxelComponentData data) =>
        {
            ecb.DestroyEntity(entity);
        });
        TerrianManager.waitForDelete = false;
    }
}
